<template>
  <div class="base-container" id="main">
    <t-form ref="form" :data="queryParams" label-width="80"
      @submit="() => { this.pagination.current = 1; this.queryParams.pageNum = 1; this.getList() }"
      @reset="() => { this.queryParams.pageNum = 1; this.queryParams.pageNum = 1; this.getList() }">
      <t-row style="border-bottom: 1px solid #E5E6EB;margin-bottom: 10px">
        <t-col :span="11" class="query-form-inline">
          <t-form-item label="科普站名称" name="name">
            <t-input v-model="queryParams.name" clearable placeholder="请输入科普站名称" style="width: 240px" />
          </t-form-item>
          <t-form-item label="申报单位名称" name="declareDeptName">
            <t-input v-model="queryParams.declareDeptName" clearable placeholder="请输入申报单位名称" style="width: 240px" />
          </t-form-item>
          <t-form-item label="基地类型" name="baseType ">
            <t-select clearable v-model="queryParams.baseType" :options="dict.type.base_type">
            </t-select>
          </t-form-item>
          <t-form-item label="科普站状态" name="baseStatus">
            <t-select clearable v-model="queryParams.baseStatus" :options="dict.type.base_status">
            </t-select>
          </t-form-item>
        </t-col>
        <t-col :span="1" class="operation-container">
          <t-button theme="primary" type="submit"> 查询</t-button>
          <t-button theme="default" variant="outline" type="reset">重置</t-button>
        </t-col>
      </t-row>
    </t-form>
    <div class="btn-group-inline">
      <t-button @click="apply" variant="outline" theme="primary">
        <t-icon name="add" slot="icon" />
        申报
      </t-button>
    </div>
    <t-table rowKey="index" size="small" :data="data" :columns="columns" hover :pagination="pagination"
      :loading="dataLoading" :sort="sort" :hideSortTips="true" @sort-change="sortChange">
      <template #name="{ row }">
        <t-link @click="dialog.header = row.name; dialog.objId = row.id; dialog.open_detail = true" hover="color"
          theme="primary">{{ row.name }}</t-link>
      </template>
      <template #baseType="{ row }">
        {{ row.baseType | filterByDict(dict.type.base_type) }}
      </template>
      <template #workPlan="{ row }">
        <div v-if="row.workPlan === 1">是</div>
        <div v-else>否</div>
      </template>
      <template #baseStatus="{ row }">
        <t-tag @click="dialog.header = '审批记录'; dialog.objId = row.id; dialog.open_approve_list = true"
          class="cursor-pointer" :theme="handleTheme(row.baseStatus, dict.type.base_status)" variant="light">
          {{ row.baseStatus | filterByDict(dict.type.base_status) }}
        </t-tag>
      </template>
      <template #op="{ row }">
        <t-button size="small" @click="toUpdate(row)" variant="text" class="t-button-link">
          <t-icon name="edit-1" slot="icon" />
          {{ row.baseStatus == 1 ? '继续申报' : '修改' }}
        </t-button>
        <t-button size="small" @click="dialog.header = row.name; dialog.objId = row.id; dialog.open_detail = true"
          variant="text" class="t-button-link">
          <t-icon name="info-circle" slot="icon" />
          详情
        </t-button>
        <t-button @click="createPDF(row)" size="small" variant="text" class="t-button-link">
          <t-icon name="file-pdf" slot="icon" />
          导出PDF
        </t-button>
        <!-- <t-button v-if="['1', '4'].includes(row.baseStatus)" size="small" @click="toUpdate(row)" variant="text"
          class="t-button-link">
          <t-icon name="edit-1" slot="icon" />
          {{ row.baseStatus == 1 ? '继续申报' : '修改' }}
        </t-button> -->
        <t-button v-if="['1', '4'].includes(row.baseStatus)" theme="danger" @click="deleteById(row)" size="small"
          variant="text" class="t-button-link">
          <t-icon name="delete" slot="icon" />
          删除
        </t-button>
      </template>
    </t-table>
    <base-detail-dialog :header="dialog.header" :obj-id="dialog.objId" :visible.sync="dialog.open_detail"
      v-if="dialog.open_detail"></base-detail-dialog>
    <approveList :header="dialog.header" :system="'kpzh'" :obj-id="dialog.objId"
      :visible.sync="dialog.open_approve_list" v-if="dialog.open_approve_list"></approveList>
  </div>
</template>

<script>
import approveList from "@/pages/bzh/approve/components/approve-list.vue";
import BaseDetailDialog from "./components/base-detail-dialog.vue";

export default {
  name: "base-index",
  dicts: ["base_type", "base_status"],
  components: {
    approveList,
    BaseDetailDialog
  },
  data () {
    return {
      deptMenuOptions: [],
      dialog: {
        header: "",
        objId: null,
        open_detail: false,
        open_approve_list: false,
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showPageSize: true,
        pageSizeOptions: [10, 20, 30, 50],
        showFirstAndLastPageBtn: false,
        onChange: (pageInfo) => {
          Object.assign(this.pagination, pageInfo)
          this.queryParams.pageNum = pageInfo.current || 1;
          this.queryParams.pageSize = pageInfo.pageSize;
          this.getList()
        },
      },
      sort: [],
      dataLoading: false,
      columns: [
        {
          colKey: "name",
          title: "科普站名称",
          align: "center",
          width: 220

        },
        {
          colKey: "declareDeptName",
          title: "申报单位",
          align: "center",
        },
        {
          colKey: "address",
          title: "科普站地址",
          align: "center",
        },
        {
          colKey: "baseType",
          title: "基地类型",
          align: "center",
        },
        {
          colKey: "legalPerson",
          title: "法定代表人",
          align: "center",
        },
        // {
        //   colKey: "communicateAddress",
        //   title: "通讯地址",
        //   align: "center",
        //   width: 160,
        // },
        // {
        //   colKey: "postCode",
        //   title: "邮编",
        //   align: "center",
        //   width: 160,
        // },
        // {
        //   colKey: "legalPerson",
        //   title: "科普站负责人",
        //   align: "center",
        //   width: 160,
        // },
        {
          colKey: "linkerPerson",
          title: "联系人",
          align: "center",
        },
        {
          colKey: "mobilePhone",
          title: "移动电话",
          align: "center",
        },
        // {
        //   colKey: "fixedPhone",
        //   title: "固定电话",
        //   align: "center",
        //   width: 160,
        // },
        // {
        //   colKey: "fax",
        //   title: "传真",
        //   align: "center",
        //   width: 160,
        // },
        {
          colKey: "email",
          title: "公共邮箱",
          align: "center",
        },
        // {
        //   colKey: "workPlan",
        //   title: "交通运输科普工作管理制度、规划或计划 有或无",
        //   align: "center",
        //   width: 160,
        // },
        // {
        //   colKey: "indoorVenues",
        //   title: "室内场馆面积/m2",
        //   align: "center",
        //   width: 160,
        // },
        // {
        //   colKey: "outdoorVenues",
        //   title: "室外场馆面积/m2",
        //   align: "center",
        //   width: 160,
        // },
        // {
        //   colKey: "lectureHall",
        //   title: "影视报告厅面积/m2",
        //   align: "center",
        //   width: 160,
        // },
        // {
        //   colKey: "openDays",
        //   title: "每年对外开放时间/天",
        //   align: "center",
        //   width: 160,
        // },
        // {
        //   colKey: "receptionPerson",
        //   title: "每年接待公众/人次",
        //   align: "center",
        //   width: 160,
        //   sorter: true,
        // },
        {
          colKey: "baseStatus",
          title: "科普站状态",
          align: "center",
          sorter: true,
          fixed: 'right'
        },
        {
          colKey: "op",
          title: "操作",
          align: "center",
          width: 240,
          fixed: 'right'
        },
      ],
      opinionVisible: true,
      data: [],

    }
  },
  created () {
    Object.assign(this.queryParams, this.handelQuery(this.queryParams, this.$route.query))
    this.getDeptTreeSelect()
    this.getList()
  },
  methods: {
    deleteById (row) {
      const confirmDia = this.$dialog.confirm({
        header: "系统提示",
        body: `是否确认删除？`,
        theme: "warning",
        onConfirm: ({ e }) => {
          confirmDia.update({
            confirmBtn: {
              loading: true,
            },
          })
          this.$api.kpzh.base.deleteBase(row.id).then(
            (res) => {
              this.getList()
              this.$message.success("删除成功");
              // 请求成功后，销毁弹框
              confirmDia.destroy();
            }
          ).catch((e) => {
            confirmDia.update({
              confirmBtn: {
                loading: false,
              },
            })
            this.$message.error(e.toString());
          });
        }
      });
    },
    createPDF (row) {
      const instance = this.$loading({
        fullscreen: true,
        attach: 'body',
        preventScrollThrough: false,
      });
      this.$api.kpzh.project.newProjectPDF(row.id).then(response => {
        window.open(response.data.fullFilePath, '_blank')
        instance.hide();
      }).catch((e) => {
        instance.hide();
        this.$message.error(e.toString());
      });
    },
    toUpdate (row) {
      sessionStorage.removeItem(`${row.id}-kpzh-science-base-activeForm`)
      const taskId = row.id;
      const activeFormList = [];
      this.$api.kpzh.base.getAll(row.id).then(response => {
        sessionStorage.removeItem(`${taskId}-kpzh-science-base-baseId`);
        if (response.data.id) {
          sessionStorage.setItem(`${taskId}-kpzh-science-base-baseId`, response.data.id);
        } else {
          activeFormList.push(0);
        }
        if (response.data.popularScienceBaseSurvey && response.data.popularScienceBaseSurvey.id) {
          sessionStorage.setItem(`${taskId}-kpzh-science-base-surveyId`, response.data.popularScienceBaseSurvey.id)
        } else {
          activeFormList.push(1);
        }
        if (response.data.popularScienceBaseContent && response.data.popularScienceBaseContent.id) {
          sessionStorage.setItem(`${taskId}-kpzh-science-base-contentId`, response.data.popularScienceBaseContent.id)
        } else {
          activeFormList.push(2);
        }
        if (response.data.popularScienceBasePictures && response.data.popularScienceBasePictures.length > 0) {
          sessionStorage.setItem(`${taskId}-kpzh-science-base-pictureId`, response.data.popularScienceBasePictures.length)
        } else {
          activeFormList.push(3);
        }
        if (response.data.popularScienceBasePublicitys && response.data.popularScienceBasePublicitys.length > 0) {
          sessionStorage.setItem(`${taskId}-kpzh-science-base-publicityId`, response.data.popularScienceBasePublicitys.length)
        } else {
          activeFormList.push(4);
        }
        if (response.data.popularScienceBaseCommentary && response.data.popularScienceBaseCommentary.id) {
          sessionStorage.setItem(`${taskId}-kpzh-science-base-commentaryId`, response.data.popularScienceBaseCommentary.id)
        } else {
          activeFormList.push(5);
        }
        if (response.data.popularScienceBaseRewards && response.data.popularScienceBaseRewards.length > 0) {
          sessionStorage.setItem(`${taskId}-kpzh-science-base-rewardId`, response.data.popularScienceBaseRewards.length)
        } else {
          activeFormList.push(6);
        }
        if (response.data.popularScienceBaseActivitys && response.data.popularScienceBaseActivitys.length > 0) {
          sessionStorage.setItem(`${taskId}-kpzh-science-base-activityId`, response.data.popularScienceBaseActivitys.length)
        } else {
          activeFormList.push(7);
        }
        if (response.data.popularScienceBaseActivitys && response.data.popularScienceBaseActivitys.length > 0) {
          sessionStorage.setItem(`${taskId}-kpzh-science-base-fileId`, response.data.popularScienceBaseActivitys.length)
        } else {
          activeFormList.push(7);
        }
        if (activeFormList.length <= 0) {
          sessionStorage.setItem(`${taskId}-kpzh-science-base-fileId`, 8); // 跳转到最后一步
        } else {
          sessionStorage.setItem(`${taskId}-kpzh-science-base-activeForm`, activeFormList[0]); // 跳转到第一个不满足
        }
        this.$router.push({ path: `/kpzh/other/science-base/base-apply/${row.id}`, query: { title: `${row.name}-科普基地申报` } });
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    apply () {
      this.$router.push(`/kpzh/other/science-base/base-apply/${Date.parse(new Date())}`);
    },
    getDeptTreeSelect () {
      return this.$api.system.dept.treeSelectAllIn().then(response => {
        this.deptMenuOptions = response.data
      }).catch(e => {
        this.$message.error(e.toString())
      })
    },
    sortChange (sort) {
      this.sort = sort;
      let orderByColumn = []
      let isAsc = []
      if (sort instanceof Array) {
        orderByColumn = sort.map(val => val.sortBy)
        isAsc = sort.map(val => val.descending ? `desc` : `asc`)
      } else {
        orderByColumn = [sort.sortBy]
        isAsc = [sort.descending ? `desc` : `asc`]
      }
      this.queryParams.orderByColumn = orderByColumn.join(",")
      this.queryParams.isAsc = isAsc.join(",")
      this.getList();
    },
    getList () {
      // [this.queryParams.startPublishDate,this.queryParams.endPublishDate] = this.queryParams.publicDateRange;
      this.dataLoading = true
      this.$api.kpzh.base.myList(this.queryParams).then(response => {
        this.pagination.total = response.total
        this.data = response.rows
        this.dataLoading = false
      }).catch(e => {
        this.dataLoading = false
        this.$message.error(e.toString())
      })
    }
  }
}
</script>

<style lang="less" scoped></style>
